<template>
  <section style="text-align: left">
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      @close="clear">
      <el-form :model="permissionForm" ref="PermissionForm">
        <el-form-item label="名字" :label-width="formLabelWidth" prop="name">
          <el-input v-model="permissionForm.name"/>
        </el-form-item>
        <el-form-item label="中文名" :label-width="formLabelWidth" prop="nameZh">
          <el-input v-model="permissionForm.nameZh"/>
        </el-form-item>
        <el-form-item label="访问路径" :label-width="formLabelWidth" prop="url">
          <el-input v-model="permissionForm.url" autocomplete="off"/>
        </el-form-item>
        <!--        <el-form-item label="组件路径" :label-width="formLabelWidth" prop="component">
                  <el-input v-model="permissionForm.component"/>
                </el-form-item>-->
        <el-form-item label="组件名" :label-width="formLabelWidth" prop="componentName">
          <el-input v-model="permissionForm.componentName"/>
        </el-form-item>
        <el-form-item label="所属位置" :label-width="formLabelWidth" prop="parentId">
          <el-checkbox-group v-model="checkParentId">
            <el-checkbox v-for="(parent,id) in parents" :label="parent.id" :key="id">
              {{ parent.nameZh }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onsubmit">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
export default {
  name: "AddOrUpdatePermission",
  data() {
    return {
      title: '',
      checkParentId: [],
      parents: [],
      dialogFormVisible: false,
      permissionForm: {
        url: '',
        name: '',
        nameZh: '',
        component: '',
        componentName: '',
        parentId: '',
      },
      formLabelWidth: '120px',
    }
  },
  methods: {
    clear() {
      this.permissionForm = {
        url: '',
        name: '',
        naneZh: '',
        component: '',
        componentName: '',
        parentId: '',
      }
      this.checkParentId = []
    },
    add(parents) {
      this.parents = parents
      this.$emit('reload')
    },
    edit(row, parents) {
      this.parents = parents
      this.checkParentId.push(row.parentId)
      // row.url = row.url.substring(8, row.url.length)
      this.permissionForm = Object.assign({}, row)
      this.$emit('reload')
    },
    onsubmit() {
      // 验证表单信息
      // this.$refs.PermissionForm.validate((valid)=>{
      //
      // })
      let data = Object.assign({}, this.permissionForm)

      if (this.checkParentId.length === 0) {
        data.parentId = 0
      } else {
        data.parentId = this.checkParentId.toString()
      }
      this.$axios.post('/permission/menu/addPermission', data).then(resp => {
        if (resp && resp.status === 200) {
          if (resp.data.code === 200) {
            this.$message({
              message: resp.data.result,
              type: "success"
            })
          }
          if (resp.data.code === 400) {
            this.$message({
              message: resp.data.result,
              type: "warning"
            })
          }
          this.clear()
          this.dialogFormVisible = false
          this.$emit('reload')
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
